//
//  PageStyleView.m
//  ebook
//
//  Created by 吴超飞 on 16/3/11.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "PageStyleView.h"
#import "MTRotatingScreen.h"

@interface PageStyleView(){
    
    UIButton *pageStyleBtn1,*pageStyleBtn2,*pageStyleBtn3,*pageStyleBtn4;
    UIButton *portraitBtn,*landscapeRightBtn;//横屏、竖屏 按钮
    UIButton *autoScrollReadingBtn;//自动阅读
    ReaderConfig *readerConfig;
    float width;
}

@end

@implementation PageStyleView

-(instancetype)initWithReaderConfig:(ReaderConfig *)config{
    
    readerConfig = config;
    if (isIpad) {
        width = 320 + 60;
    } else {
        width = MTScreenW;
    }
    self = [super initWithFrame:CGRectMake(0, 0, width, 60+50+60)];

    if (self) {
        [self drawView];
    }
    
    return self;
}

- (void)drawView {
    
    float edgeLeft = 20;
    float space = 20;
    float btnWidth = (width - 20*2 - 3*space)/4;
    if ([MTRotatingScreen isOrientationLandscape] && !isIpad) {
        edgeLeft = width/6;
        btnWidth = (width-edgeLeft*2-3*space)/4;
    }
    
    pageStyleBtn1 = [self buttonFrame:CGRectMake(edgeLeft, 20, btnWidth, 40) tittleName:@"仿真" withTag:1];
    pageStyleBtn2 = [self buttonFrame:CGRectMake(CGRectGetMaxX(pageStyleBtn1.frame)+space, 20, btnWidth, 40) tittleName:@"连环" withTag:2];
    pageStyleBtn3 = [self buttonFrame:CGRectMake(CGRectGetMaxX(pageStyleBtn2.frame)+space, 20, btnWidth, 40) tittleName:@"上下" withTag:3];
    pageStyleBtn4 = [self buttonFrame:CGRectMake(CGRectGetMaxX(pageStyleBtn3.frame)+space, 20, btnWidth, 40) tittleName:@"无动画" withTag:4];
    autoScrollReadingBtn = [self btnFrame:CGRectMake(edgeLeft, CGRectGetMaxY(pageStyleBtn1.frame)+space, width-edgeLeft*2, 40) title:@"自动阅读" tag:1];
    portraitBtn = [self btnFrame:CGRectMake(edgeLeft, CGRectGetMaxY(autoScrollReadingBtn.frame)+space, (width-edgeLeft*2-space)/2, 40) title:@"竖屏" tag:2];
    landscapeRightBtn = [self btnFrame:CGRectMake(CGRectGetMaxX(portraitBtn.frame)+space, CGRectGetMinY(portraitBtn.frame), portraitBtn.width, portraitBtn.height) title:@"横屏" tag:3];
    
    [self addSubview:pageStyleBtn1];
    [self addSubview:pageStyleBtn2];
    [self addSubview:pageStyleBtn3];
    [self addSubview:pageStyleBtn4];
    [self addSubview:autoScrollReadingBtn];
    [self addSubview:portraitBtn];
    [self addSubview:landscapeRightBtn];
    
    if (readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStylePageCurl&&readerConfig.PageNavigationOrientation==UIPageViewControllerNavigationOrientationHorizontal&&!readerConfig.IsPageTransitionNoAnimate) {
        [self buttonClick:pageStyleBtn1];
    }else if (readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStyleScroll&&readerConfig.PageNavigationOrientation==UIPageViewControllerNavigationOrientationHorizontal&&!readerConfig.IsPageTransitionNoAnimate){
        [self buttonClick:pageStyleBtn2];
    }else if (readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStyleScroll&&readerConfig.PageNavigationOrientation==UIPageViewControllerNavigationOrientationVertical&&!readerConfig.IsPageTransitionNoAnimate){
        [self buttonClick:pageStyleBtn3];
    }else if (readerConfig.PageTransitionStyle==UIPageViewControllerTransitionStylePageCurl&&readerConfig.PageNavigationOrientation==UIPageViewControllerNavigationOrientationHorizontal&&readerConfig.IsPageTransitionNoAnimate){
        [self buttonClick:pageStyleBtn4];
    }
    
    //设置横屏、竖屏的按钮状态
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        [self btnClick:landscapeRightBtn];
    } else {
        [self btnClick:portraitBtn];
    }
}

#pragma mark - 翻页效果选择button
-(UIButton *)buttonFrame:(CGRect)frame tittleName:(NSString *)tittle withTag:(NSInteger)tag{
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    UIColor *selectedColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    btn.tag = tag;
    btn.layer.borderColor = btn.isSelected ? selectedColor.CGColor:normalColor.CGColor;
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 3.0f;
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:MTConfigColor(@"MTReaderToolBar_textTint_Color") forState:UIControlStateNormal];
    [btn setTitle:tittle forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(void)buttonClick:(UIButton *)btn{
   
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    UIColor *selectedColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    pageStyleBtn1.layer.borderColor = btn.tag==1?selectedColor.CGColor:normalColor.CGColor;
    pageStyleBtn2.layer.borderColor = btn.tag==2?selectedColor.CGColor:normalColor.CGColor;
    pageStyleBtn3.layer.borderColor = btn.tag==3?selectedColor.CGColor:normalColor.CGColor;
    pageStyleBtn4.layer.borderColor = btn.tag==4?selectedColor.CGColor:normalColor.CGColor;
    
    if ([self.delegate respondsToSelector:@selector(pageStyleView:changePageStyle:)]) {
        [self.delegate pageStyleView:self changePageStyle:(int)(btn.tag-1)];
    }
}

#pragma mark - 竖屏、横屏 按钮
- (UIButton *)btnFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    btn.tag = tag;
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    btn.layer.borderColor = btn.isSelected ? selectColor.CGColor:normalColor.CGColor;
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 3.0f;
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:MTConfigColor(@"MTReaderToolBar_textTint_Color") forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    if (tag==1) {
        [btn addTarget:self action:@selector(autoScrollReadingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

- (void)autoScrollReadingBtnClick:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(pageStyleView:autoReadingBtnClick:)]) {
        [self.delegate pageStyleView:self autoReadingBtnClick:btn];
    }
}

- (void)btnClick:(UIButton *)btn {
    
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    portraitBtn.layer.borderColor = btn.tag==2?selectColor.CGColor:normalColor.CGColor;
    landscapeRightBtn.layer.borderColor = btn.tag==3?selectColor.CGColor:normalColor.CGColor;
    
    if ([self.delegate respondsToSelector:@selector(pageStyleView:orientationIsLandscape:)]) {
        [self.delegate pageStyleView:self orientationIsLandscape:btn.tag==3];
    }
}

@end
